<?php
/**
 * 根据实际交易情况更新校检商家信用
 *
 * 命令：
 * bash$ ./cli credit
 * $Id: credit.php 4 2011-12-15 09:28:04Z 周永隆 $
 */
class CLI_Credit extends CLI
{

    /**
     * 脚本主方法
     *
     * @return void
     */
    public function main()
    {
        //记录开始时间
        $time_start = microtime(TRUE);

        $offset=0;
        $per_count=100;

        //循环获取所有可用店铺ID
        while (true)
        {
            $rows = DB::select('shop_id','member_id')
                ->from('shops')
                ->order_by('shop_id', 'ASC')
                ->limit($per_count)
                ->offset($offset)
                ->fetch_all();

            //记录集遍历完毕
            if(count($rows)<1){
                //计算脚本运行耗时
                $time_script = round( microtime(TRUE) - $time_start , 3);
                CLI::output("Done at:" . date('Y-m-d H:i:s') ." \nscript time:" . $time_script);
                die();
            }

            //根据记录集进行计算
            $j=0;
            foreach($rows as $item){
                $j++;
                if($item['shop_id']){
                    // 描述服务发货打分数量
                    $rate_count = DB::select(DB::expr('COUNT(*)'))
                        ->from('shop_rates')
                        ->where('shop_id','=',$item['shop_id'])
                        ->fetch_one();

                    $rate = DB::select(
                            array(DB::expr('SUM(des_score)'), 'des'),
                            array(DB::expr('SUM(service_score)'), 'service'),
                            array(DB::expr('SUM(express_score)'), 'express')
                        )
                        ->from('shop_rates')
                        ->where('shop_id','=',$item['shop_id'])
                        ->fetch_row();

                    // 好评总数
                    $good = DB::select(DB::expr('COUNT(*)'))
                            ->from('order_rates')
                            ->where('shop_id','=',$item['shop_id'])
                            ->where('buyer_deleted','=',0)
                            ->where('buyer_flag','=',1)
                            ->where('buyer_comment_score','=','1')
                            ->fetch_one();
                    // 差评总数
                    $bad = DB::select(DB::expr('COUNT(*)'))
                            ->from('order_rates')
                            ->where('shop_id','=',$item['shop_id'])
                            ->where('buyer_deleted','=',0)
                            ->where('buyer_flag','=',1)
                            ->where('buyer_comment_score','=','-1')
                            ->fetch_one();
                    // 中评总数
                    $mid = DB::select(DB::expr('COUNT(*)'))
                            ->from('order_rates')
                            ->where('shop_id','=',$item['shop_id'])
                            ->where('buyer_deleted','=',0)
                            ->where('buyer_flag','=',1)
                            ->where('buyer_comment_score','=','0')
                            ->fetch_one();
                    // 更新数据
                    $sql = DB::update('members')
                    ->set(
                        array(
                            'good_num'=> $good,
                            'bad_num'=> $bad,
                            'medium_num'=> $mid,
                            'credit_score'=> ($good-$bad),
                        )
                    )
                    ->where('member_id','=',$item['member_id'])
                    ->execute();

                    if ($rate_count > 0)
                    {
                        DB::update('shops')
                        ->set(array(
                            'des_score' => $rate['des'] / $rate_count,
                            'service_score' => $rate['service'] / $rate_count,
                            'express_score' => $rate['express'] / $rate_count,
                        ))
                        ->where('shop_id','=',$item['shop_id'])
                        ->execute();
                    }



                    CLI::output  ($offset + $j . ",shop_id:" .  $item['shop_id'] . ",good:".$good.",bad:".$bad.",mid:".$mid.",creidt:".($good-$bad)."\n");
                }

            }

            $offset=$offset+$per_count;
        }

    }



}